package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcBerthDAO;
import com.trkj.dao.jpa.CcCasrhistoryDAO;
import com.trkj.dao.jpa.CcElectornicCheckDAO;
import com.trkj.dao.jpa.CcElectornicPrescribeDAO;
import com.trkj.dao.jpa.CcHospitalElectronicDAO;
import com.trkj.dao.jpa.CcOuthospitalDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.CcPrepaymentDAO;
import com.trkj.dao.jpa.CcWardDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.pojos.CcBerth;
import com.trkj.pojos.CcCasrhistory;
import com.trkj.pojos.CcElectornicCheck;
import com.trkj.pojos.CcHospitalElectronic;
import com.trkj.pojos.CcOuthospital;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.CcPrepayment;
import com.trkj.pojos.CcWard;
import com.trkj.pojos.Tbstaff;
import com.trkj.vo.CcOuthospitalVo;

@Service
@Transactional
public class CcOuthospitalService {

	@Autowired
	private CcOuthospitalDAO ccOuthospDAO;
	@Autowired
	private IYzrTbstaffDAO staffDAO;
	@Autowired
	private CcPatientDAO patientDAO;
	@Autowired
	private CcPrepaymentDAO ccPrepaymentDAO;
	@Autowired
	private CcBerthDAO ccBerthDAO;
	@Autowired
	private CcWardDAO ccWardDAO;
	@Autowired
	private CcCasrhistoryDAO cccDAO;
	@Autowired
	private CcElectornicCheckDAO ccelecDAO;
	@Autowired
	private CcHospitalElectronicDAO cchospeleDAO;
	@Autowired
	private CcElectornicPrescribeDAO celepreDAO;
	public void upOkCcOuthospital(String outid,String dorctorid,String pid){
		CcOuthospital out=ccOuthospDAO.findOne(Long.valueOf(outid));
		out.setOutDoctorid(new BigDecimal(dorctorid));
		out.setHandledate(new Date());
		out.setHandleremarks("已处理");
		CcPatient cp=patientDAO.findByOneCcPatientid(Long.parseLong(pid));
		if(cp!=null){
		ccOuthospDAO.save(out);
		cp.setPremarks("已出院");
		patientDAO.save(cp);
		List<CcHospitalElectronic> hospele=cchospeleDAO.findByheSickid(null, new BigDecimal(pid));
		for(CcHospitalElectronic hosp:hospele){
			if(hosp.getHeType().equals("检查")){
				List<CcElectornicCheck> elecheck=ccelecDAO.queryByEleId(new BigDecimal(hosp.getHeId()));
				for(CcElectornicCheck celee:elecheck){
					if(celee.getCheckrameks()==null){
						ccelecDAO.delete(celee);
					}
				}
				hosp.setHospeleRemarks("已完成");
				cchospeleDAO.save(hosp);
			}
		}
		if(cp.getPaberthid()!=null){
			CcBerth cb=ccBerthDAO.findOne(cp.getPaberthid().longValue());
			cb.setBerthstate("空");
			cb.setBpId(null);
			
			ccBerthDAO.save(cb);
			CcWard cw=ccWardDAO.findOne(cb.getBwardno().longValue());
			cw.setHremarks("未住满");
			cw.setWpeople(Integer.parseInt(cw.getWpeople())-1+"");
			ccWardDAO.save(cw);
			
			CcCasrhistory ccc=new  CcCasrhistory();
			ccc.setAdmissiondate(cp.getPpdate());
			ccc.setCasepatientid(cp.getPatientid()+"");
			ccc.setOutdate(new Date());
			ccc.setCberthid(cp.getPaberthid());
			ccc.setInofficeid(cp.getPofficesid()+"");
			ccc.setOutofficeid(cp.getPofficesid()+"");
			ccc.setOutgenreno(cb.getBwardno());
			cccDAO.save(ccc);
		}
		
		CcPrepayment cpp=ccPrepaymentDAO.findBypatient(new BigDecimal(cp.getPatientid()));
		cpp.setPpmoney(new BigDecimal(0));
		cpp.setPpstatc("离开");
		ccPrepaymentDAO.save(cpp);
		}else{
			System.err.println(pid);
		}
	}
	public long addCcOuthospital(BigDecimal outPid){
		long i=ccOuthospDAO.findByoutPid(outPid);
		if(i==0){
			CcOuthospital co=new CcOuthospital();
			co.setOutPid(outPid);
			co.setHandleremarks("未处理");
			co.setOuthodate(new Date());
			ccOuthospDAO.save(co);
			i=0;
		}
		return i;
	}
	public Map<String, Object> queryAllOuthosp(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		Map<String, Object> map =new HashMap<String, Object>();
		List<CcOuthospital> list=ccOuthospDAO.queryByAll(pr); 
		map.put("total", ccOuthospDAO.count());
		map.put("rows", ccOuthospitalToCcOuthospitalVo(list));
		return map;
	}
	public CcPrepayment queryByoutid(String outid){
		CcOuthospital out=ccOuthospDAO.findOne(Long.valueOf(outid));
		CcPrepayment cpp=ccPrepaymentDAO.findBypatient(out.getOutPid());
		return cpp;
		
	}
	public void updateCcOuthosp(String outid,String outDoctorid){
		CcOuthospital out=ccOuthospDAO.findOne(Long.valueOf(outid));
		out.setOutDoctorid(new BigDecimal(outDoctorid));
		out.setHandledate(new Date());
		out.setHandleremarks("已驳回");
		ccOuthospDAO.save(out);
	}
	private List<CcOuthospitalVo> ccOuthospitalToCcOuthospitalVo(List<CcOuthospital> list){
		List<CcOuthospitalVo> listVo=new ArrayList<CcOuthospitalVo>();
		for(CcOuthospital op:list){
			CcOuthospitalVo opVo=new CcOuthospitalVo();
			opVo.setHandledate(op.getHandledate());
			opVo.setHandleremarks(op.getHandleremarks());
			opVo.setOutDoctorid(op.getOutDoctorid());
			opVo.setOuthodate(op.getOuthodate());
			opVo.setOutId(op.getOutId());
			opVo.setOutPid(op.getOutPid());
			if(opVo.getOutDoctorid()!=null){
				Tbstaff staff=staffDAO.findOne(opVo.getOutDoctorid().longValue());
				opVo.setStaffid(staff.getStaffNo());
				opVo.setStaffName(staff.getStaffName());
			}
			CcPatient cp=patientDAO.findOne(opVo.getOutPid().longValue());
			if(cp!=null){
				opVo.setPid(cp.getPatientid());
				opVo.setPname(cp.getPname());
			}
			listVo.add(opVo);
		}
		return listVo;
	}
}
